package core;
import ibm.util.Replace;

import java.io.UnsupportedEncodingException;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import resources.core.FunctionsHelper;
import core.exceptions.ResultFailureException;
import dbops.Table;
/**
 * Description   : Functional Test Script
 * @author angel.tsvetkov
 */
public class Functions extends FunctionsHelper
{
	private static Calendar calendar = Calendar.getInstance();

	// Array which represent days from the week
	private static String[] DaysOfWeek = { "Sunday", "Monday", "Tuesday",
			"Wednesday", "Thursday", "Friday", "Saturday" };

	// Array which represent months
	private static String[] Months = { "January", "February", "March", "April",
			"May", "June", "July", "August", "September", "October",
			"November", "December" };

	// Array which represent quarters
	private static String[] Quarters = { "1st Quarter", "2nd Quarter",
			"3rd Quarter", "4th Quarter", };

	// Get current quarter by month
	public static String getCurrentQuarter() {
		String currentQuareter = null;

		switch (calendar.get(Calendar.MONTH) + 1) {
		case 1:
		case 2:
		case 3: {
			currentQuareter = Quarters[0];
		}
			break;

		case 4:
		case 5:
		case 6: {
			currentQuareter = Quarters[1];
		}
			break;

		case 7:
		case 8:
		case 9: {
			currentQuareter = Quarters[2];
		}
			break;

		case 10:
		case 11:
		case 12: {
			currentQuareter = Quarters[3];
		}
			break;
		}

		return currentQuareter;
	}

	// Return current month - Text
	public static String getCurrentMonth() {
		return Months[calendar.get(Calendar.MONTH)];
	}

	// Return current month - Number
	public static String getCurrentMonthNumber() {
		return Integer.toString(calendar.get(Calendar.MONTH) + 1);
	}

	// Return current year
	public static String getCurrentYear() {
		return Integer.toString(calendar.get(Calendar.YEAR));
	}

	// Return previous year
	public static String getPreviousYear() {
		return Integer.toString(calendar.get(Calendar.YEAR) - 1);
	}

	// Return next year
	public static String getNextYear() {
		return Integer.toString(calendar.get(Calendar.YEAR) + 1);
	}

	// Return current week
	public static String getCurrentWeek() {
		return Integer.toString((calendar.get(Calendar.WEEK_OF_YEAR)));
	}

	// Return current day of week
	public static String getCurrentDay() {
		return DaysOfWeek[calendar.get(Calendar.DAY_OF_WEEK) - 1];
	}
	
	//Return the count of days in the current month
	public static int getDaysInCurrentMonth() {
		return calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
	}

	// Return current day of month
	public static String getCurrentDayOfMonth() {
		return Integer.toString(calendar.get(Calendar.DAY_OF_MONTH));
	}

	// Return current week number of month
	public static String getWeekNumberInCurrentMonth() {
		return Integer.toString(calendar.get(Calendar.WEEK_OF_MONTH));
	}

	// Set minimal days in week
	public static void setMinimalDaysInWeek() {
		calendar.setMinimalDaysInFirstWeek(7);
	}

	// Set first day of week
	public static void setFirstDayInWeek() {
		calendar.setFirstDayOfWeek(2);
	}

	// get minimal number of days in week
	public static String getMinimalDaysInFirstWeek() {
		return Integer.toString(calendar.getMinimalDaysInFirstWeek());
	}

	// Get first day in week
	public static String getFirstDayInWeek() {
		return Integer.toString(calendar.getFirstDayOfWeek());
	}
	
	public static double formatDouble(double value, int digitsAfterDot) {
		double formatedValue = 0.0;
		
		formatedValue = Double.valueOf(formatDoubleToString(value, digitsAfterDot));
		
    	return formatedValue;
	}
	
	public static String formatDoubleToString(double value, int digitsAfterDot) 
	{  
		StringBuilder strBuilder = new StringBuilder();
		String pattern;
		String formatedValue;
		
		strBuilder.append("0.");
		
		for (int i = 0; i < digitsAfterDot; i++){
			strBuilder.append("0");
		}
		pattern = strBuilder.toString();
    	DecimalFormat decFormat = new DecimalFormat(pattern);
    	formatedValue = decFormat.format(value);
    	
    	return formatedValue;
	} 

	public static String encrypt(String word, int mask) {
		String encrypted = null;
		try {
			byte[] bytes = word.getBytes("UTF-8");
			byte[] encryptedBytes = new byte[bytes.length];
			for (int i = 0; i < bytes.length; i++) {
				byte current = bytes[i];
				encryptedBytes[i] = (byte) (current ^ mask);
			}
			encrypted = new String(encryptedBytes);
		} catch (UnsupportedEncodingException e) {
			System.err.println("Unsupported Encoding Exception");
		}
		
		return encrypted;
	}
	
	public static String getTomorrow()
	{
		Calendar calendar = Calendar.getInstance();
		calendar.add(Calendar.DATE, 1);
		SimpleDateFormat sdf = new SimpleDateFormat("MM/dd/yyyy");
		return sdf.format(calendar.getTime());
	}
	
    public static void compare(String a, String b) throws ResultFailureException
    {
    	if(!a.equals(b))
    	{
    		throw new ResultFailureException("Unexpected Data: " + a + " - " + b);
    	}
    }
    
	public static String escapeSpecialChars(String s) {
		if (s == null)
			return null;
		// must do "\" first, so we don't escape the escape
		String[] re_chars = { "\\", "{", "}", "$", "~", "(", ")", "+", "^",
				"%", ".", "*", "?", "[", "]", "|" };
		for (int i = 0; i < re_chars.length; i++) {
			s = Replace.replace(s, re_chars[i], "\\" + re_chars[i]);
		}
		return s;
	}
}

